package com.jxbeidou.system.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.mybatis.service.impl.BaseServiceImpl;
import com.jxbeidou.core.mybatis.convert.PageResultConvert;
import com.jxbeidou.system.user.convert.UserPostConvert;
import com.jxbeidou.system.user.entity.UserPostDO;
import com.jxbeidou.system.user.query.UserPostQuery;
import com.jxbeidou.system.user.vo.UserPostVO;
import com.jxbeidou.system.user.mapper.UserPostMapper;
import com.jxbeidou.system.user.service.UserPostService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户岗位关系表
 *
 * @author 杜晓祥 
 * @since 1.0.0 2023-12-14
 */
@Service
@AllArgsConstructor
public class UserPostServiceImpl extends BaseServiceImpl<UserPostMapper, UserPostDO> implements UserPostService {

    @Override
    public PageResult<UserPostVO> page(UserPostQuery query) {
        IPage<UserPostDO> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        PageResult<UserPostVO> result = PageResultConvert.convert(page, entityList -> UserPostConvert.INSTANCE.convertList(entityList));
        return result;
    }

    private LambdaQueryWrapper<UserPostDO> getWrapper(UserPostQuery query){
        LambdaQueryWrapper<UserPostDO> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(UserPostVO vo) {
        UserPostDO entity = UserPostConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(UserPostVO vo) {
        UserPostDO entity = UserPostConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}